﻿using BCCommon;
using BCData.Common.SystemConfiguration;
using BCDto.Common.SystemConfiguration;
using BCDto.TMS.Driver.SystemConfiguration;
using BCEntity.Common.EntityNameConst;
using BCEntity.Common.SystemConfiguration;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.Common.SystemConfiguration
{
    public class SystemConfigurationService : ISystemConfigurationService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IApplicationContextService applicationContextService;
        private readonly IRedisService redisService;
        private readonly ISystemConfigurationData systemConfigurationData;
        private readonly string key = string.Format("{0}", EntityNameConst.SystemConfigurationEntity);
        private readonly string driverAppKey = string.Format("{0}", EntityNameConst.DriverAppSystemConfigurationEntity);

        public SystemConfigurationService(IDatabaseContext databaseContext,
            IApplicationContextService applicationContextService,
            IRedisService redisService,
            ISystemConfigurationData systemConfigurationData)
        {
            this.databaseContext = databaseContext;
            this.applicationContextService = applicationContextService;
            this.redisService = redisService;
            this.systemConfigurationData = systemConfigurationData;
        }

        #region 检查系统配置是否存在
        public bool Exists(int systemConfigurationId)
        {
            return this.systemConfigurationData.Exists(systemConfigurationId).GetAwaiter().GetResult();
        }
        #endregion

        #region 修改系统配置
        public SystemConfigurationDto Update(SystemConfigurationPutDto putDto)
        {
            if (!this.Exists(putDto.SystemConfigurationId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<SystemConfigurationEntity>();
            entity.EditAdminName = applicationContextService.Admin.FullName;
            entity.EditAdminId = applicationContextService.Admin.AdminId;
            var result = this.systemConfigurationData.Update(entity).GetAwaiter().GetResult().As<SystemConfigurationDto>();
            string cacheKey = this.redisService.GetKey(key, entity.SystemConfigurationId);
            this.redisService.DeleteAsync(cacheKey);
            this.redisService.DeleteAsync(driverAppKey);
            return result;
        }
        #endregion

        #region 获取系统配置
        public SystemConfigurationDto Get()
        {
            string cacheKey = this.redisService.GetKey(key);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.systemConfigurationData.GetEntity().GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromDays(1));
            return result.As<SystemConfigurationDto>();
        }
        #endregion

        #region 获取系统配置
        public DriverAppSystemConfigurationDto GetDriverAppConfig()
        {
            string cacheKey = this.redisService.GetKey(string.Format("{0}", EntityNameConst.DriverAppSystemConfigurationEntity));
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.systemConfigurationData.GetEntity().GetAwaiter().GetResult();
                return data.As<DriverAppSystemConfigurationDto>();
            }, TimeSpan.FromDays(1));
            return result;
        }
        #endregion
    }
}
